<?php

namespace App\Admin\Actions\Goods;

use App\Models\AdminUserModel;
use App\Models\yb\GoodsModel;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;



class NetworkGoods extends BatchAction
{
    protected $selector = '.network-goods-copy';

    public function handle(Collection $collection, Request $request)
    {
        $network_id = $request->get('network_id');

        try {
            foreach ($collection as $model) {
                GoodsModel::insert([
                    'network_id' => $network_id,
                    'goods_type' => $model->goods_type,
                    'goods_cover' => $model->goods_cover,
                    'goods_banner' => json_encode($model->goods_banner),
                    'goods_title' => $model->goods_title,
                    'goods_unit' => $model->goods_unit,
                    'goods_specs' => $model->goods_specs,
                    'goods_detail' => $model->goods_detail,
                    'is_barrel_deposit' => $model->is_barrel_deposit,
                    'barrel_deposit' => $model->barrel_deposit,
                    'distribution_fee' => $model->distribution_fee,
                    'goods_delivery_type' => $model->goods_delivery_type,
                    'return_credit' => $model->return_credit,
                    'freight' => $model->freight,
                    'is_client_show' => $model->is_client_show,
                    'weight' => $model->weight,
                    'goods_warehouse_id' => $model->id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                ]);
            }
            return $this->response()->success('操作成功')->refresh();

        } catch (Exception $e) {
            return $this->response()->error('产生错误：'.$e->getMessage());
        }

    }

    public function form()
    {
        $this->select('network_id', '所属网点')->options(AdminUserModel::selectOptions())->rules('required');
    }

    public function html()
    {
        return "<a class='network-goods-copy btn btn-sm btn-warning' style='margin-left: 5px;'><i class='fa fa-info-circle'></i>&nbsp;网点选品</a>";
    }

}
